package org.jeecg.modules.mes.service.impl;

import java.util.List;
import java.util.Date;

import org.jeecg.modules.mes.entity.MesPieceRateStandard;
import org.jeecg.modules.mes.mapper.MesPieceRateStandardMapper;
import org.jeecg.modules.mes.service.IMesPieceRateStandardService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 计件工资标准表
 * @Author: jeecg-boot
 * @Date: 2025-01-01
 * @Version: V1.0
 */
@Service
@Slf4j
public class MesPieceRateStandardServiceImpl extends ServiceImpl<MesPieceRateStandardMapper, MesPieceRateStandard> implements IMesPieceRateStandardService {

    @Autowired
    private MesPieceRateStandardMapper pieceRateStandardMapper;

    @Override
    public MesPieceRateStandard getRateStandard(String workTypeId, String productCategory) {
        try {
            return pieceRateStandardMapper.getRateStandard(workTypeId, productCategory);
        } catch (Exception e) {
            log.error("获取计件标准失败: workTypeId={}, productCategory={}", workTypeId, productCategory, e);
            return null;
        }
    }

    @Override
    public List<MesPieceRateStandard> getRateStandardsByWorkType(String workTypeId) {
        try {
            return pieceRateStandardMapper.getRateStandardsByWorkType(workTypeId);
        } catch (Exception e) {
            log.error("根据工种获取计件标准失败: workTypeId={}", workTypeId, e);
            return null;
        }
    }

    @Override
    public List<MesPieceRateStandard> getValidRateStandards() {
        try {
            return pieceRateStandardMapper.getValidRateStandards();
        } catch (Exception e) {
            log.error("获取有效计件标准失败", e);
            return null;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateRateStandard(MesPieceRateStandard standard) {
        try {
            // 设置生效日期默认值
            if (standard.getEffectiveDate() == null) {
                standard.setEffectiveDate(new Date());
            }
            
            // 设置默认状态
            if (standard.getStatus() == null) {
                standard.setStatus(1);
            }

            // 设置默认难度系数
            if (standard.getDifficultyFactor() == null) {
                standard.setDifficultyFactor(java.math.BigDecimal.ONE);
            }

            return this.saveOrUpdate(standard);
        } catch (Exception e) {
            log.error("保存计件标准失败", e);
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchImportRateStandards(List<MesPieceRateStandard> standards) {
        try {
            for (MesPieceRateStandard standard : standards) {
                // 设置默认值
                if (standard.getEffectiveDate() == null) {
                    standard.setEffectiveDate(new Date());
                }
                if (standard.getStatus() == null) {
                    standard.setStatus(1);
                }
                if (standard.getDifficultyFactor() == null) {
                    standard.setDifficultyFactor(java.math.BigDecimal.ONE);
                }
            }
            
            return this.saveBatch(standards);
        } catch (Exception e) {
            log.error("批量导入计件标准失败", e);
            return false;
        }
    }
}
